package org.example.SlApi.RyxxApi;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.codec.net.URLCodec;
import org.example.JycInfo;
import org.example.utils.ReadConf;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import static org.example.CookieHandlingFilter.cookies;
import static org.example.RestClient.givenWithCookies;
import static org.example.RestClient.loginAndUpdateCookies;
import static org.example.SlApi.JbxxApi.JbxxbcApi.initJyclist;

public class LlyAPI {//人员信息-高管董事一号信息保存
    public static void main(String[] args) throws IOException {
        gg1API();
    }
    private static List<JycInfo> JycList = new ArrayList<>();

    public static int gg1API() throws IOException {
        // 登录并更新cookies

        loginAndUpdateCookies();

        RestAssured.baseURI = ReadConf.getHost(); // 设置基础URI
        List<JycInfo> jycInfoList = initJyclist(); // 调用方法来获取列表
        if (!jycInfoList.isEmpty()) {
            JycInfo firstJycInfo = jycInfoList.get(0); // 获取列表中的第一个元素
            String pripid = firstJycInfo.getPripid(); // 使用实际的getter方法
            String sbid = firstJycInfo.getSbid();
            String entname = firstJycInfo.getEntname();
            String qylxdl = firstJycInfo.getQylxdl();

            URLCodec urlCodec = new URLCodec();
            String encodedParam = URLEncoder.encode(entname, "UTF-8");

            String data = "ywtSldj.do?method=saveFddbr&entid="+pripid+"&entname="+encodedParam+"&pripid="+pripid+"&sbid="+sbid+"&qylxdl="+qylxdl+"&type=1&sfzdrtype=0&llry=S_NAME_%3A%E6%A0%97%E4%BC%9A_%2CS_CERNO_%3A142726197009280616_%2CS_XB_%3A1_%2CS_MOBTEL_%3A13568547854_%2CS_EMAIL_%3A174%40qq.com_%2CS_ADDR_%3A%E4%BD%86%E8%BF%98%E7%AE%97%E5%BF%AB%E6%8E%A5%E7%94%B5%E8%AF%9D_%2CS_CERTYPE_%3A10_%2CS_PRIPID_%3A"+pripid+"_%2CS_RYTYPE_%3A1_%2CS_NAME_OCR_%3A_%2CS_CERNO_OCR_%3A_%2CS_ADDRESS_OCR_%3A_%2CS_VALID_UNTIL_OCR_%3A&zmpath_ll=&fmpath_ll=&hzpath_ll=&hkbpath_ll=&baseZheng_ll=&baseFan_ll=&baseHZ_ll=&baseHKB_ll=&currentTab_ryxx=wscwry_box_id&csrftoken=";
            Response response = givenWithCookies()

                    .header("Content-Type","multipart/form-data; boundary=----WebKitFormBoundary6FLIcZL51kmdTR9Y")
//               .cookies(cookies)

                    .log().all()
                    .post(data);
            System.out.println(cookies);
            int statusCode = Integer.parseInt(String.valueOf(response.getStatusCode()));
            System.out.println(statusCode);
            String body = response.getBody().asString();
            System.out.println(body);
            if (statusCode == 302) {

                String redirectUrl = response.getHeader("Location");
                System.out.println(redirectUrl);
                if (redirectUrl != null) {
                    Response finalResponse = givenWithCookies()
                            .log().all()
                            .when()
                            .get(redirectUrl);
                    int statusCode1 = finalResponse.getStatusCode();
                    System.out.println("Status Code: " + statusCode);


            return 0;

        }
        return 0;
    }
        }
        return 0;
    }
}